<html>
<head><meta charset="utf-8"><title>Transmuting Rc&lt;MaybeUninit&lt;T&gt;&gt; to Rc&lt;()&gt; · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html">Transmuting Rc&lt;MaybeUninit&lt;T&gt;&gt; to Rc&lt;()&gt;</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220979275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220979275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220979275">(Dec 26 2020 at 21:10)</a>:</h4>
<p>I have a struct doing type erasure, where I store <code>Rc&lt;()&gt;</code> for type erasure purposes. I know the validity of references is under debate, but given that <code>Rc</code> stores a pointer, so long as I patch up the type before I do any accesses, I should be good right?</p>



<a name="220979289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220979289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220979289">(Dec 26 2020 at 21:12)</a>:</h4>
<p>Uh... so certainly this will go wrong if you drop that <code>Rc</code>^^</p>



<a name="220979329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220979329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220979329">(Dec 26 2020 at 21:12)</a>:</h4>
<p>beyond that, this might work, but seems like violating the <code>Rc</code> abstraction... as in, since <code>Rc</code> doesn't document such a guarantee, it might change in the future in ways that fundamentally break this assumption</p>



<a name="220979333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220979333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220979333">(Dec 26 2020 at 21:13)</a>:</h4>
<p>It's <code>ManuallyDrop</code> (I'm only looking at this because Miri caught that exact bug :P)</p>



<a name="220979342"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220979342" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220979342">(Dec 26 2020 at 21:13)</a>:</h4>
<p>:D</p>



<a name="220979349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220979349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220979349">(Dec 26 2020 at 21:13)</a>:</h4>
<p>so, <code>ManuallyDrop&lt;Rc&lt;()&gt;&gt;</code> or what do you mean?</p>



<a name="220979388"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220979388" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220979388">(Dec 26 2020 at 21:14)</a>:</h4>
<p>Yes, <code>ManuallyDrop&lt;Rc&lt;()&gt;&gt;</code>.</p>



<a name="220979851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220979851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220979851">(Dec 26 2020 at 21:29)</a>:</h4>
<p>Is there a reason to store that over just *const () or so? And then implementing Clone / Drop appropriately to manage the reference count?</p>



<a name="220979857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220979857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220979857">(Dec 26 2020 at 21:29)</a>:</h4>
<p>you'll want to opt-out of send/sync etc anyway</p>



<a name="220979923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220979923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220979923">(Dec 26 2020 at 21:31)</a>:</h4>
<p>I need to support the <code>Rc</code> methods like <code>strong_count</code>, and it just seems more error prone to convert the pointer back and make sure I didn't make any mistakes.</p>



<a name="220979965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220979965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220979965">(Dec 26 2020 at 21:32)</a>:</h4>
<p>I would be very concerned with an accidental clone() call or something that leads to UB, but not exactly sure what you're doing</p>



<a name="220979995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220979995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220979995">(Dec 26 2020 at 21:33)</a>:</h4>
<p>and any new functions added on Rc will need to be vetted anyway to make sure they're safe to call..</p>



<a name="220980063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220980063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220980063">(Dec 26 2020 at 21:35)</a>:</h4>
<p>Hmm fair. It's basically a custom type-erased DST (nudge nudge), but I guess raw pointers might be a better fit here (are you suggesting <code>*mut Rc&lt;()&gt;</code> or <code>*mut ()</code>)?</p>



<a name="220980116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220980116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220980116">(Dec 26 2020 at 21:36)</a>:</h4>
<p>*const () would be reasonable, I guess. It's definitely <em>not</em> a raw pointer to an Rc so the first seems quite misleading</p>



<a name="220980506"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220980506" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220980506">(Dec 26 2020 at 21:48)</a>:</h4>
<p>alternatively you could add a newtype around <code>ManuallyDrop&lt;Rc&lt;()&gt;&gt;</code> that doesnt implement <code>Deref</code>; then you can also avoid accidental interactions</p>



<a name="220981082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220981082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220981082">(Dec 26 2020 at 22:05)</a>:</h4>
<p>True - not sure how much benefit that gives you, though.</p>



<a name="220993841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220993841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220993841">(Dec 27 2020 at 05:01)</a>:</h4>
<p>Another random question, you said somewhere that (on mobile and lost the tab),</p>
<div class="codehilite"><pre><span></span><code>Notice that Rc::into_raw returns a ptr with &quot;shared ref&quot; provenance. So actually writing to that pointer is UB. (In fact, this likely makes Rc::from_raw(Rc::into_raw(rc)).get_mut() unsound .)
</code></pre></div>
<p>But I swear I've casted the result of <code>into_raw</code> to a mutable pointer and wirtten to it without miri complaining. Is this still the case?</p>



<a name="220994179"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/220994179" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#220994179">(Dec 27 2020 at 05:11)</a>:</h4>
<p>Nevermind, seems like a relic of an API bug. Cyclic refernces are quite difficult.</p>



<a name="221007569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/221007569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#221007569">(Dec 27 2020 at 12:47)</a>:</h4>
<p>uh, I lost context here^^ not sure when/where I said that</p>



<a name="221007607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/221007607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#221007607">(Dec 27 2020 at 12:48)</a>:</h4>
<p>but yeah that sounds like a bugs that has been fixed</p>



<a name="221007617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting%20Rc%3CMaybeUninit%3CT%3E%3E%20to%20Rc%3C%28%29%3E/near/221007617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Transmuting.20Rc.3CMaybeUninit.3CT.3E.3E.20to.20Rc.3C().3E.html#221007617">(Dec 27 2020 at 12:49)</a>:</h4>
<blockquote>
<p>But I swear I've casted the result of into_raw to a mutable pointer and wirtten to it without miri complaining. Is this still the case?</p>
</blockquote>
<p>FWIW, in such cases it could also be a good idea to try running Miri with <code>-Zmiri-track-raw-pointers</code>. This enables a more strict mode that doesnt support int-to-ptr casts, but can find more cases of problematic code.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>